package admin

import (
	"fmt"
	"game-server/app/common/request"
	"game-server/app/common/response"
	"game-server/app/services"
	adminservices "game-server/app/services/admin"
	"game-server/global"
	"game-server/seeders"
	"game-server/utils"

	"github.com/gin-gonic/gin"
	"github.com/golang-jwt/jwt"
	"go.uber.org/zap"
)

// InitData
func InitData(c *gin.Context) {
	seeders.InitData()
	response.Success(c, nil)
}

// Login 登录
// @Summary 登录
// @Description 登录
// @Tags 系统用户
// @Accept json
// @Produce json
// @Param data body request.SysUserLoginRequest true "登录信息"
// @Router /gameServer/admin/login [post]
func Login(c *gin.Context) {
	var form request.SysUserLoginRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	global.App.Log.Info("系统用户登录form", zap.Any("form", form))

	user, err := adminservices.SysUserService.Login(form)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	token, err, _ := services.JwtService.CreateToken(services.AdminGuardName, user)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, gin.H{
		"token": token,
		// "user":  user,
	})
}

// SysUserCreate 创建用户
// @Summary 创建用户
// @Description 创建用户
// @Tags 系统用户
// @Accept json
// @Produce json
// @Param data body request.SysUserCreateRequest true "用户信息"
// @Router /gameServer/admin/user/create [post]
func SysUserCreate(c *gin.Context) {
	var form request.SysUserCreateRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	user, err := adminservices.SysUserService.SysUserCreate(form)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, user)
}

// // SysUserList 用户列表
// @Summary 用户列表
// @Description 带条件分页查询用户信息
// @Tags 系统用户
// @Accept json
// @Produce json
// @Param data body request.SysUserListRequest true "查询参数"
// @Router /gameServer/admin/user/list [post]
func SysUserList(c *gin.Context) {
	var form request.SysUserListRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	list, total, err := adminservices.SysUserService.GetUserList(form)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, gin.H{
		"list":  list,
		"total": total,
	})
}

// SysUserUpdate 更新用户
// @Summary 更新用户
// @Description 更新用户信息
// @Tags 系统用户
// @Accept json
// @Produce json
// @Param data body request.SysUserUpdateRequest true "用户信息"
// @Router /gameServer/admin/user/updateUser [put]
func SysUserUpdate(c *gin.Context) {
	var form request.SysUserUpdateRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	if err := adminservices.SysUserService.UpdateUser(form); err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, nil)
}

// SysUserUpdateStatus 更新用户状态
// @Summary 更新用户状态
// @Description 启用或禁用用户
// @Tags 系统用户
// @Accept json
// @Produce json
// @Param data body request.SysUserStatusRequest true "用户状态信息"
// @Router /gameServer/admin/user/updateStatus [put]
func SysUserUpdateStatus(c *gin.Context) {
	var form request.SysUserStatusRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	if err := adminservices.SysUserService.UpdateUserStatus(form); err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, nil)
}

// SysUserGet 根据ID获取用户信息
// @Summary 获取用户信息
// @Description 根据用户ID获取用户详细信息
// @Tags 系统用户
// @Accept json
// @Produce json
// @Param id path int true "用户ID"
// @Router /gameServer/admin/user/{id} [get]
func SysUserGet(c *gin.Context) {
	id := c.Param("id")
	user, err := adminservices.SysUserService.GetUserById(id)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, user)
}

// SysUserDelete 删除用户
// @Summary 删除用户
// @Description 根据用户ID删除用户
// @Tags 系统用户
// @Accept json
// @Produce json
// @Param id path int true "用户ID"
// @Router /gameServer/admin/user/{id} [delete]
func SysUserDelete(c *gin.Context) {
	id := c.Param("id")
	if err := adminservices.SysUserService.DeleteUser(id); err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, nil)
}

// SysUserInfo 获取当前用户信息
// @Summary 获取当前用户信息
// @Description 获取当前登录用户的详细信息，包括菜单权限和角色
// @Tags 系统用户
// @Accept json
// @Produce json
// @Router /gameServer/admin/user/info [get]
func SysUserInfo(c *gin.Context) {
	// 从上下文获取当前用户ID
	userId, _ := c.Get("id")
	if userId == 0 || userId == nil {
		response.BusinessFail(c, "用户未登录")
		return
	}

	// 获取用户信息
	user, err := adminservices.SysUserService.GetUserById(fmt.Sprint(userId))
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	numId, err := utils.Str2Uint(fmt.Sprint(userId))
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	// 获取用户的菜单权限
	menus, err := adminservices.SysMenuService.GetUserMenus(numId)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	// 获取用户的角色信息
	roles, err := adminservices.SysRoleService.GetUserAssignedRoles(fmt.Sprint(userId))
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}

	response.Success(c, gin.H{
		"user":  user,
		"roles": roles,
		"menus": menus,
	})
}

// Logout 登出
// @Summary 登出
// @Description 登出
// @Tags 系统用户
// @Accept json
// @Produce json
// @Router /gameServer/admin/logout [post]
func Logout(c *gin.Context) {
	// todo
	err := services.JwtService.JoinBlackList(c.Keys["token"].(*jwt.Token))
	if err != nil {
		response.BusinessFail(c, "登出失败")
		return
	}
	response.Success(c, nil)
}
